﻿/*
 * Created by SharpDevelop.
 * User: user
 * Date: 21.09.2012
 * Time: 14:30
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Windows.Forms;

namespace ServicesPayments
{

     
	
	/// <summary>
	/// Description of CatalogueFormBase.
	/// </summary>
	public partial class frmCatalogueTemplate : Form
	{

	    private FormOpenMode _formOpenMode = FormOpenMode.Read;

	    public FormOpenMode FormMode
	    {
	        get { return _formOpenMode; }
            set { _formOpenMode = value; }
	    }

		public IList DataSource
		{
			get { return (IList)dataGridView1.DataSource; }
			set { dataGridView1.DataSource = value; }
		}
		
		private frmCatalogueTemplate()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			SetIcons();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//

			this.dataGridView1.AutoGenerateColumns = false;
			//ProcessGridViewMetadata();
		}

		public frmCatalogueTemplate(Type gridObjectType):this()
		{
			ProcessGridViewMetadata(gridObjectType);
		}

        

		
		protected void ProcessGridViewMetadata(Type catalogEntryType)
		{
			if (catalogEntryType.BaseType == typeof(CatalogEntryBase))
			{
				foreach (var property in catalogEntryType.GetProperties())
				{
					//var gcattr = property.GetCustomAttribute(typeof (GridColumnAttribute)) as GridColumnAttribute;
					foreach (var gcattr in property.GetCustomAttributes(false).OfType<GridColumnAttribute>())
					{
						//int index=
							dataGridView1.Columns.Add(new DataGridViewColumn
													  {
														  CellTemplate = property.PropertyType == typeof (bool)
																		? (DataGridViewCell) new DataGridViewCheckBoxCell()
																		: new DataGridViewTextBoxCell(),
														  HeaderText = gcattr.Caption,
														  DataPropertyName = property.Name,
														  DisplayIndex = gcattr.ColumnIndex,
														  Visible = gcattr.Visible//,
														  //ReadOnly=(FormMode== FormOpenMode.Read)?true:false

													  });
												
							
						
					}
					
					
				}
			}
			
			dataGridView1.ReadOnly=	(FormMode== FormOpenMode.Read)?true:false;
		}

        private void frmCatalogueTemplate_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Visible = false;
        }

        #region --PUBLIC--
        public new void  Show(FormOpenMode formMode)
        {
            this.FormMode = formMode;
            this.ShowDialog();
        }


        public void ChangeFormMode(FormOpenMode newMode)
        {
        	FormModeChangeEventArgs args= new FormModeChangeEventArgs(this.FormMode,newMode);
        		
        		
        	
        	
            if(OnModeChanging!=null)
            	OnModeChanging(this,args);
            
			
            this.FormMode = newMode;
            switch (newMode) {
            	case FormOpenMode.Create:
            		
            		break;
            	case FormOpenMode.Edit:
            		
            		dataGridView1.ReadOnly=false;
            		
            		
            		break;
            	case FormOpenMode.Read:
            		
            		break;
            	default:
            		throw new Exception("Invalid value for FormOpenMode");
            }

            
            	               
            if(OnModeChanged!=null)
            	OnModeChanged(this,args);
        }

	    #endregion

        #region --EVENT--

	    

	    public event FormModeChangeHandler OnModeChanged;
	    public event FormModeChangeHandler OnModeChanging;
	    

	   

	    #endregion



		
		void TsbtnEditClick(object sender, EventArgs e)
		{
			ChangeFormMode(FormOpenMode.Edit);
		}
	}

	#region --Event Args--
	
    public class FormModeChangeEventArgs : EventArgs
    {
        public FormOpenMode OldMode { get; set; }
        public FormOpenMode NewMode { get; set; }

        public FormModeChangeEventArgs(FormOpenMode oldMode, FormOpenMode newMode)
        {
            this.NewMode = newMode;
            this.OldMode = oldMode;
        }
    }

    public delegate void FormModeChangeHandler(object sender, FormModeChangeEventArgs e);
    
    #endregion
}
